<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>品牌列表</title>
    <link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <!--搜索表单-->
    <el-form :inline="true" :model="brand" class="demo-form-inline">
        <el-form-item label="当前状态">
            <el-select v-model="brand.status" placeholder="当前状态">
                <el-option label="请选择" value=""></el-option>
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="品牌名称">
            <el-input v-model="brand.brandName" placeholder="品牌名称"></el-input>
        </el-form-item>
        <el-form-item label="企业名称">
            <el-input v-model="brand.companyName" placeholder="企业名称"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>

    <!--批量删除和新增按钮-->

    <el-row>
        <el-button type="danger" plain @click="deleteByIds()">批量删除</el-button>
        <el-button type="primary" plain  @click="dialogVisible = true">新增</el-button>
        <el-button plain  onclick="location.href='brandLog.html'">查看日志</el-button>
    </el-row>

    <!--添加对话框-->
    <el-dialog
            title="添加品牌"
            :visible.sync="dialogVisible"
            width="30%">

        <el-form ref="addFormData" :model="addFormData" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="addFormData.brandName"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="addFormData.companyName"></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="addFormData.ordered"></el-input>
            </el-form-item>
            <el-form-item label="描述">
                <el-input type="textarea" v-model="addFormData.description"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch v-model="addFormData.status"
                           active-value="1"
                           inactive-value="0"></el-switch>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="add">立即添加</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
	
	<!--修改对话框-->
    <el-dialog
            title="修改品牌"
            :visible.sync="editDialogVisible"
            width="30%">

        <el-form ref="editFormData" :model="editFormData" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="editFormData.brandName"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="editFormData.companyName"></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="editFormData.ordered"></el-input>
            </el-form-item>
            <el-form-item label="描述">
                <el-input type="textarea" v-model="editFormData.description"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch v-model="editFormData.status"
                           active-value="1"
                           inactive-value="0"></el-switch>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="update">立即修改</el-button>
                <el-button @click="editDialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <!--展示数据的table表格-->
    <template>
        <el-table
                ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>
            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    label="排序"
                    align="center"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column
                    prop="statusStr"
                    label="当前状态"
                    align="center"
                    show-overflow-tooltip>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-button
                            type="primary"
                            size="mini"
                            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </template>

    <!--分页条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
</div>
<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<script src="js/axios-0.18.0.js"></script>
<script>
    new Vue({
        el:"#app",
        data() {
            return {
                //定义搜索表单模型数据
                brand: {
                    status: '',
                    brandName: '',
                    companyName: '',
                },
                //新增对话框显示或隐藏模型数据
                dialogVisible: false,
                //新增对话框表单模型数据
                addFormData: {
                    brandName: '',
                    companyName: '',
                    ordered: '',
                    description: '',
                    status: '0'
                },
                //编辑对话框显示或隐藏模型数据
                editDialogVisible: false,
                //编辑对话框表单模型数据
                editFormData: {
                    brandName: '',
                    companyName: '',
                    ordered: '',
                    description: '',
                    status: '1'
                },
                //表格模型数据
                tableData: [],
                //保存复选框选中条目数据对象数组
                multipleSelection: [],
                //分页条当前页数
                currentPage: 1,
                //分页条总页数模型数据
                total:0,
                //分页条当前页数模型数据
                pageSize:5
            }
        },
        mounted(){
            //页面加载完成之后发送异步请求进行分页查询
            this.selectByPage();
        },
        methods: {
            //批量删除的方法
            deleteByIds(){
                console.log(this.multipleSelection)
                //弹出确认框
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //点击了确定按钮
                    //1 发送post请求，批量删除
                    axios.post("brand/deleteByIds",this.multipleSelection).then(resp=>{
                        //2 处理响应结果
                        if(resp.data=="success"){
                            //2.1 提示“删除成功”
                            this.$message.success('恭喜你，批量删除成功');
                            //2.2 重新分页查询
                            this.selectByPage();
                        }
                    });
                });
                selectByPage();
            },
            //定义带条件的分页查询的方法
            selectByPage(){
                axios.post(`brand/selectByPage?currentPage=${this.currentPage}&pageSize=${this.pageSize}`,this.brand).then(resp=>{
                    //处理响应结果,给对象的模型数据赋值
                    console.log(resp.data);  //其实就是pageInfo对象
                    //1 表格中展示当前页数据；给tableData赋值
                    this.tableData=resp.data.list;
                    //2 展示分页条：给total赋值
                    this.total=resp.data.total;
                });
            },
            //搜索表单查询按钮单击事件绑定的函数
            onSubmit() {
                console.log(this.brand);
                //调用分页查询方法
                this.selectByPage();

            },
            //新增对话框"立即添加"按钮单击事件绑定函数
            add() {
                console.log(this.addFormData);
                //发送异步请求，将addFormData表单模型数据发送给服务器
                axios.post("brand/add",this.addFormData).then(resp=>{
                    //处理响应结果
                    //1 判断是否添加成功
                    if(resp.data=="success"){
                        //2 隐藏弹出框
                        this.dialogVisible=false;
                        //3 重新分页查询
                        this.selectByPage();
                        //4 清空新增品牌模型数据addFormData
                        this.addFormData= {};
                        //5 弹出警告框表示新增成功
                       /* this.$message({
                            message: '恭喜你，新增品牌成功',
                            type: 'success'
                        });*/
                        //简写
                        this.$message.success('恭喜你，新增品牌成功');
                    }

                });
            },
            //表格复选框选中时间绑定函数
            handleSelectionChange(val) {
                this.multipleSelection=[];
                //遍历val数组，获取选中条目brand对象的id，保存到multipleSelection
                for (let i = 0; i < val.length; i++) {
                    this.multipleSelection[i]=val[i].id
                }
                console.log(this.multipleSelection);

            },
            //表格中“修改”和“删除”按钮单击事件函数
            handleEdit(index, row) {
                //index表示当前条目索引，row表示当前条目数据对象
                console.log(index, row);
                //展示修改对话框
                this.editDialogVisible=true;
                //给表单模型数据赋值，展示要修改的数据
                this.editFormData={
                    id:row.id,
                    brandName:row.brandName,
                    companyName:row.companyName,
                    ordered:row.ordered,
                    description:row.description,
                    status:row.status
                };
                //status属性只有转换成字符串开关才可以正常显示。
                this.editFormData.status+="";

            },
            handleDelete(index, row) {
                console.log(index, row);
                //index表示当前条目索引，row表示当前条目数据对象，row.id表示当前条目数据对象的id值
                this.multipleSelection=[row.id];
                //调用批量删除的方法
                this.deleteByIds();

            },
            //点击编辑弹出框上的"立即修改"按钮单击事件函数
            update(){
                console.log(this.editFormData);
                //发送异步请求，将editFormData传递给服务器
                axios.post("brand/update",this.editFormData).then(resp=>{
                    if(resp.data=="success"){
                        //2 隐藏弹出框
                        this.editDialogVisible=false;
                        //3 重新分页查询
                        this.selectByPage();
                        //4 清空新增品牌模型数据addFormData
                        this.editFormData= {};
                        //5 弹出警告框表示新增成功
                        /* this.$message({
                             message: '恭喜你，新增品牌成功',
                             type: 'success'
                         });*/
                        //简写
                        this.$message.success('恭喜你，修改品牌成功');
                    }


                });
            },
            //分页条事件
            //选择每页条数触发的事件函数
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                //修改每页条数
                this.pageSize=val;
                //调用selectByPage方法发送异步请求
                this.selectByPage();
            },
            //点击页码、上一页、下一页触发的事件函数
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.currentPage=val;//修改当前页数
                //调用selectByPage方法发送异步请求，获取当前页数据
                this.selectByPage();
            }
        }
    });
</script>

</body>
</html>